package com.ts.api.module.channel.entity;

import com.gitee.apanlh.util.net.addr.IpUtils;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;

/**
 *  渠道请求对象
 */
@Data
public class ChannelRequest {

    /** ip */
    private String ip;
    /** userAgent */
    private UserAgent userAgent;
    /** sessionId */
    private String sessionId;
    /** 用户token */
    private String token;
    /** 表单或注册的获取流程状态 */
    private String tableFlow;

    /**
     * 构造函数
     */
    public ChannelRequest() {
        super();
    }

    /**
     * 构造函数
     * @param ip
     * @param userAgent
     * @param sessionId
     */
    public ChannelRequest(String ip, UserAgent userAgent, String sessionId, String token, String tableFlow) {
        this.ip = ip;
        this.userAgent = userAgent;
        this.sessionId = sessionId;
        this.token = token;
        this.tableFlow = tableFlow;
    }

    /**
     *  创建访问对象
     *
     *  @param  request
     *  @return ChannelRequest
     */
    public static ChannelRequest create(HttpServletRequest request) {
        String ipAddr = IpUtils.getIpAddr(request);
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        String sessionId = request.getSession().getId();
        String token = request.getHeader("token");
        String tableFlow = request.getHeader("tableFlow");
        return new ChannelRequest(ipAddr, userAgent, sessionId, token, tableFlow);
    }
}
